package cn.iocoder.yudao.module.crm.service.cluepool;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.crm.controller.admin.cluepool.vo.CluePoolPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.cluepool.vo.CluePoolSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.clue.CrmClueDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.cluepool.CluePoolDO;
import cn.iocoder.yudao.module.crm.dal.mysql.clue.CrmClueMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.cluepool.CluePoolMapper;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CLUE_POOL_DELETE_REJECT;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CLUE_POOL_NOT_EXISTS;

/**
 * 线索公海 Service 实现类
 *
 * @author 数创万维
 */
@Service
@Validated
public class CluePoolServiceImpl implements CluePoolService {

    @Resource
    private CluePoolMapper cluePoolMapper;

    @Resource
    private CrmClueMapper crmClueMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createCluePool(CluePoolSaveReqVO createReqVO) {
        // 插入
        CluePoolDO cluePool = BeanUtils.toBean(createReqVO, CluePoolDO.class);
        cluePoolMapper.insert(cluePool);
        // 返回
        return cluePool.getId();
    }

    @Override
    public void updateCluePool(CluePoolSaveReqVO updateReqVO) {
        // 校验存在
        validateCluePoolExists(updateReqVO.getId());
        // 更新
        CluePoolDO updateObj = BeanUtils.toBean(updateReqVO, CluePoolDO.class);
        cluePoolMapper.updateById(updateObj);
    }

    @Override
    public void deleteCluePool(Long id) {
        // 校验存在
        validateCluePoolExists(id);
        // 校验公海下存在线索
        validateCluePoolHasClue(id);
        // 删除
        cluePoolMapper.deleteById(id);
    }

    private void validateCluePoolHasClue(Long id) {
        if (crmClueMapper.selectCount(CrmClueDO::getPoolId, id) > 0) {
            throw exception(CLUE_POOL_DELETE_REJECT);
        }
    }

    private void validateCluePoolExists(Long id) {
        if (cluePoolMapper.selectById(id) == null) {
            throw exception(CLUE_POOL_NOT_EXISTS);
        }
    }

    @Override
    public CluePoolDO getCluePool(Long id) {
        return cluePoolMapper.selectById(id);
    }

    @Override
    public PageResult<CluePoolDO> getCluePoolPage(CluePoolPageReqVO pageReqVO) {
        return cluePoolMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CluePoolDO> getEnableClueList(Long userId) {

        AdminUserRespDTO user = adminUserApi.getUser(userId);

        List<CluePoolDO> allList = cluePoolMapper.selectList(new LambdaQueryWrapperX<CluePoolDO>()
                .or(w -> w.eq(CluePoolDO::getType, 0))
                .or(w -> w.like(CluePoolDO::getDeptIds, StrUtil.concat(true, ",", String.valueOf(user.getDeptId()), ",")))
                .or(w -> w.like(CluePoolDO::getUserIds, StrUtil.concat(true, ",", String.valueOf(user.getId()), ",")))
        );

        return allList;
    }

    @Override
    public List<CluePoolDO> getEnableClueListByTenant(Long tenantId) {
        return TenantUtils.execute(tenantId, () -> cluePoolMapper.selectList());
    }
}